<%@ page import="util.DBUtil" %>
<%@ page import="jdk.nashorn.internal.ir.RuntimeNode" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="com.alipay.api.AlipayClient" %>
<%@ page import="com.alipay.api.DefaultAlipayClient" %>
<%@ page import="util.AlipayConfig" %>
<%@ page import="com.alipay.api.request.AlipayTradePagePayRequest" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    /*向支付宝接口发起支付请求*/
    //1.获得订单信息
    String id = request.getParameter("id");
    String sql = "select * from tb_order where id =" + id;
    DBUtil dbUtil = new DBUtil();
    ResultSet rs = dbUtil.query(sql);
    rs.next();

    //2.创建与支付对接的对象
    //2.1 获得初始化的AlipayClient
    AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);

    //2.2 设置请求参数
    AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
    alipayRequest.setReturnUrl(AlipayConfig.return_url);
    alipayRequest.setNotifyUrl(AlipayConfig.notify_url);

    //2.3设置订单内容有关的json参数
    String out_order_no = rs.getString("order_no");
    String total_amount = rs.getString("total_money");
    String subject      = "jju-eshp-test";
    String body         = "sale-test";
    //将上面的四个参数组合成一个json字符串
    String content = "{\"out_trade_no\":\"" + out_order_no + "\","
            + "\"total_amount\":\"" + total_amount + "\","
            + "\"subject\":\"" + subject + "\","
            + "\"body\":\"" + body + "\","
            + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}";
    alipayRequest.setBizContent(content);//设置json参数
    //3.发起支付请求到支付宝，并获得返回结果
    String ret =alipayClient.pageExecute(alipayRequest).getBody();
    //4.显示结果
    out.print(ret);
%>
</body>
</html>
